package com.fyx.timedTask;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fyx.config.FiveMongoConfig;
import com.fyx.config.FourthMongoConfig;
import com.fyx.config.NinethMongoConfig;
import com.fyx.entity.*;
import com.fyx.mapper.SysDictDetailMapper;
import com.fyx.until.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 日报记录
 */
@Slf4j
@Component(value="DayDataTask")
public class DayDataTask{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Resource(name = FourthMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate fourthMongoTemplate;

    @Resource(name = FiveMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate fiveMongoTemplate;

    @Resource(name = NinethMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate ninethMongoTemplate;

    @Autowired
    private SysDictDetailMapper sysDictDetailMapper;

    public void run(){
        record();
        log.info("日报记录任务：执行完毕=======================");
    }

    public void record(){
        long beginTime = DateUtil.getDayBegin().getTime()/1000;
        long nowTime = new Date().getTime()/1000;
        if (beginTime == nowTime){//凌晨
            beginTime =  DateUtil.getBeginDayOfYesterday().getTime()/1000;
        }
        if(nowTime<(beginTime+900)){  //15分钟内
            beginTime =  DateUtil.getBeginDayOfYesterday().getTime()/1000;
        }
      //  beginTime=DateUtil.getBeginDayOfYesterday().getTime()/1000;
        long endTime = beginTime+3600*24;
        DayDataEntity dayDataEntity = new DayDataEntity();
        QueryWrapper<SysDictDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_id", 16);
        wrapper.ge("dict_sort", 900).le("dict_sort",920);
        wrapper.orderByDesc("dict_sort");
        List<SysDictDetail> list1 = sysDictDetailMapper.selectList(wrapper);
        SysDictDetail sysDictDetail1 = new SysDictDetail();
        sysDictDetail1.setValue("total");
        sysDictDetail1.setDictSort(999);
        list1.add(sysDictDetail1);
        sysDictDetail1 = new SysDictDetail();
        sysDictDetail1.setValue("totalcl");
        sysDictDetail1.setDictSort(998);
        list1.add(sysDictDetail1);
        sysDictDetail1 = new SysDictDetail();
        sysDictDetail1.setValue("totalapp");
        sysDictDetail1.setDictSort(997);
        list1.add(sysDictDetail1);
        for(SysDictDetail sysDictDetail:list1) {
            dayDataEntity = new DayDataEntity();
            dayDataEntity.setTime(beginTime);
            dayDataEntity.setChannel(sysDictDetail.getValue());
            //红中麻将充值统计

            dayDataEntity.setAllMoney(getMoney( "all",dayDataEntity.getChannel(),beginTime,endTime,null,null,null,false));

            //苹果端充值
            dayDataEntity.setAppleHzmjMoney(getMoney( "all",dayDataEntity.getChannel(),beginTime,endTime,null,null,"ios",false));

            //安卓端充值
            dayDataEntity.setAndroidHzmjMoney(getMoney( "all",dayDataEntity.getChannel(),beginTime,endTime,null,null,"android",false));

            //PC端充值
            dayDataEntity.setPcHzmjMoney(getMoney( "all",dayDataEntity.getChannel(),beginTime,endTime,null,null,"pc",false));


            //红中麻将日新增用户
            dayDataEntity.setNewUserCount(getNewUser( "all",dayDataEntity.getChannel(), beginTime, endTime, null, null, null, false));

            //红中麻将ios日新增用户
            dayDataEntity.setIosnewUser(getNewUser( "all",dayDataEntity.getChannel(), beginTime, endTime, null, null, "ios", false));

            //红中麻将android日新增用户
            dayDataEntity.setAndroidnewUser(getNewUser( "all",dayDataEntity.getChannel(), beginTime, endTime, null, null, "android", false));

            dayDataEntity.setPcUserCount(getNewUser( "all",dayDataEntity.getChannel(), beginTime, endTime, null, null, "pc", false));

            //红中麻将日活跃
            dayDataEntity.setDayActivity(getActive( "all",dayDataEntity.getChannel(), beginTime, endTime, null, null, null, false));

            //红中麻将安卓日活跃
            dayDataEntity.setAndroidActivity(getActive( "all",dayDataEntity.getChannel(), beginTime, endTime, null, null, "android", false));

            //红中麻将苹果日活跃
            dayDataEntity.setIosActivity(getActive( "all",dayDataEntity.getChannel(), beginTime, endTime, null, null, "ios", false));

            dayDataEntity.setPcActivity(getActive( "all",dayDataEntity.getChannel(), beginTime, endTime, null, null, "pc", false));

            //发燃
//            dayDataEntity.setFaranpay(getMoney(dayDataEntity.getChannel(), beginTime, endTime, "h5", "_faran", null, false));
//            dayDataEntity.setFaranActivity(getActive(dayDataEntity.getChannel(), beginTime, endTime, "h5", "_faran", null, false));
//            dayDataEntity.setFaranUserCount(getNewUser(dayDataEntity.getChannel(), beginTime, endTime, "h5", "_faran", null, false));

            if(sysDictDetail.getDictSort()==988||sysDictDetail.getDictSort()==910) {
                dayDataEntity.setWxMiniGamePay(getMoney("clmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_wechat", null, false));
                dayDataEntity.setWxActivity(getActive("clmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_wechat", null, false));
                dayDataEntity.setWxUserCount(getNewUser("clmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_wechat", null, false));
                //发燃

                dayDataEntity.setZuiyoupay(getMoney("clmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_zuiyou", null, false));
                dayDataEntity.setZuiyouActivity(getActive("clmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_zuiyou", null, false));
                dayDataEntity.setZuiyouUserCount(getNewUser("clmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_zuiyou", null, false));

                dayDataEntity.setQqMiniGamePay(getMoney("clmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_qq", null, false));
                dayDataEntity.setQqActivity(getActive("clmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_qq", null, false));
                dayDataEntity.setQqUserCount(getNewUser("clmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_qq", null, false));

            }
            else if(sysDictDetail.getDictSort()==987||sysDictDetail.getDictSort()==920){
                dayDataEntity.setDouyinpay(getMoney("app",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_wechat", null, false));
                dayDataEntity.setDouyinActivity(getActive("app",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_wechat", null, false));
                dayDataEntity.setDouyinUserCount(getNewUser("app",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_wechat", null, false));
                //发燃

                dayDataEntity.setKuaishoupay(getMoney("app",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_zuiyou", null, false));
                dayDataEntity.setKuaishouActivity(getActive("app",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_zuiyou", null, false));
                dayDataEntity.setKuaishouUserCount(getNewUser("app",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_zuiyou", null, false));

                dayDataEntity.setIosapppay(getMoney("app",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_qq", null, false));
                dayDataEntity.setIosappActivity(getActive("app",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_qq", null, false));
                dayDataEntity.setIosappUserCount(getNewUser("app",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_qq", null, false));

            }
            else {
                dayDataEntity.setQqMiniGamePay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_qq", null, false));

                dayDataEntity.setSevenkpay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_7k7k", null, false));

                dayDataEntity.setIqiyih5pay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_iqiyih5", null, false));

                dayDataEntity.setTcypay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_tcy", null, false));

                dayDataEntity.setMeituanPay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_meituan", null, true));

                dayDataEntity.setMeituanRePay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_meituan_re", null, false));

                dayDataEntity.setWxMiniGamePay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_wechat", null, false));

                dayDataEntity.setIqiyiPay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_iqiyi", null, true));

                dayDataEntity.setBytedancepay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_tiktok", null, false));

//                dayDataEntity.setDouyinpay(getMoney(dayDataEntity.getChannel(), beginTime, endTime, null, "_topon", null, false));

                dayDataEntity.setMmypay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_mmy", null, false));

                dayDataEntity.setCaipiaopay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_500caipiao", null, false));

                dayDataEntity.setSwjoyPay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_swjoy", null, false));

                dayDataEntity.setVivoPay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_vivo", null, true));

                dayDataEntity.setAlipay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_alipay", null, false));

                if(dayDataEntity.getChannel().equals("hzmjxl_total")){
                    dayDataEntity.setXunleipay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_xunleih5", null, false));
                    dayDataEntity.setXunleiActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_xunleih5", null, false));
                    dayDataEntity.setXunleiUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_xunleih5", null, false));
                }else {
                    dayDataEntity.setXunleiActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_xunlei", null, false));
                    dayDataEntity.setXunleiUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_xunlei", null, false));
                    dayDataEntity.setXunleipay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_xunlei", null, false));
                }
                //百度充值
                dayDataEntity.setBaidupay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_baidu", "android", false));

           //     dayDataEntity.setBaiduIospay(getMoney(dayDataEntity.getChannel(), beginTime, endTime, null, "_baidu", "ios", false));

                //vivoad包充值统计
                dayDataEntity.setVivoAdPay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_vivoad", null, false));

                //闪电玩充值
                dayDataEntity.setSdwpay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_sdwh5", null, false));

                //小米充值
                dayDataEntity.setMipay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_xiaomi", null, false));

                //苹果渠道充值
                dayDataEntity.setApplepay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_official_ios", null, false));

                //华为充值
                dayDataEntity.setHuaweipay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_huawei", null, false));

                //华为h5pay
                dayDataEntity.setHuaweih5pay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_huawei", null, false));


                dayDataEntity.setTvcoocaapay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_tvcoocaa", null, false));

                dayDataEntity.setTvhisensepay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_tvhisense", null, false));

                dayDataEntity.setTvhuanpay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_tvhuan", null, false));

                //红中麻将oppoH5统计
                dayDataEntity.setOppoPay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_oppo", null, false));

                //红中麻将oppoMb统计
                dayDataEntity.setOppoMbPay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_oppo", null, false));

                //红中麻将jd统计
                dayDataEntity.setJdPay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_jd", null, false));

                //红中麻将自营统计
//                dayDataEntity.setOfficial(getMoney(dayDataEntity.getChannel(), beginTime, endTime, null, "_official", null, true));

                dayDataEntity.setMilanbarpay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_milanbar", null, false));

                //小帅
                dayDataEntity.setLfjpay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_xs", null, false));
                dayDataEntity.setLfjActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_xs", null, false));
                dayDataEntity.setLfjUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_xs", null, false));

                //360
                dayDataEntity.setQihoopay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_qihoo", null, false));
                dayDataEntity.setQihooActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_qihoo", null, false));
                dayDataEntity.setQihooUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_qihoo", null, false));

                //中彩
                dayDataEntity.setZhongcaipay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_zhongcai", null, false));
                dayDataEntity.setZhongcaiActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_zhongcai", null, false));
                dayDataEntity.setZhongcaiUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_zhongcai", null, false));

                //有料
                dayDataEntity.setShengyepay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_shengye", null, false));
                dayDataEntity.setShengyeActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_shengye", null, false));
                dayDataEntity.setShengyeUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_shengye", null, false));

                //奇趣
                dayDataEntity.setQuickpay(getMoney("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_quick", null, false));
                dayDataEntity.setQuickActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_quick", null, false));
                dayDataEntity.setQuickUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_quick", null, false));

                //红中麻将QQ日活跃
                dayDataEntity.setQqActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_qq", null, false));

                dayDataEntity.setSevenkActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_7k7k", null, false));

                //红中麻将wx日活跃
                dayDataEntity.setWxActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_wechat", null, false));

                //麻将iqiyi日活跃
                dayDataEntity.setIqiyiActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_iqiyi", null, true));

                dayDataEntity.setIqiyih5Activity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_iqiyih5", null, false));

                dayDataEntity.setTcyActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_tcy", null, false));

                dayDataEntity.setMmyActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_mmy", null, false));

                dayDataEntity.setBytedanceActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_tiktok", null, false));
                //彩票日活跃
                dayDataEntity.setCaipiaoActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_500caipiao", null, false));

                //红中麻将mt日活跃
                dayDataEntity.setMeituanActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_meituan", null, true));

                //红中麻将mtRe日活跃
                dayDataEntity.setMeituanReActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_meituan_re", null, false));

                //红中麻将oppo-h5日活跃
                dayDataEntity.setOppoActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_oppo", null, false));

                //红中麻将oppo日活跃
                dayDataEntity.setOppoMbActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_oppo", null, false));


                //红中麻将京东日活跃
                dayDataEntity.setJdActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_jd", null, false));

                //红中麻将顺网日活跃
                dayDataEntity.setSwjoyActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_swjoy", null, false));

                //红中麻将闪电玩日活跃
                dayDataEntity.setSdwActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_sdwh5", null, false));

                //红中麻将苹果渠道日活跃
                dayDataEntity.setAppleActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_official_ios", null, false));

                //红中麻将华为日活跃
                dayDataEntity.setHuaweiActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_huawei", null, false));

                //红中麻将H5华为日活跃
                dayDataEntity.setHuaweiH5Activity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_huawei", null, false));

                dayDataEntity.setTvhuanActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_tvhuan", null, false));

                dayDataEntity.setTvhisenseActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_tvhisense", null, false));

                dayDataEntity.setTvcoocaaActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_tvcoocaa", null, false));

                //红中麻将小米日活跃
                dayDataEntity.setMiActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_xiaomi", null, false));

                //红中麻将vivo日活跃
                dayDataEntity.setVivoActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_vivo", null, true));

                //红中麻将vivoad日活跃
                dayDataEntity.setVivoAdActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_vivoad", null, false));

                //红中麻将自营日活跃
//                dayDataEntity.setOfficialActivity(getActive(dayDataEntity.getChannel(), beginTime, endTime, null, "_official", null, true));

                //2345日活跃
                dayDataEntity.setAliActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_alipay", null, false));


                //百度日活跃
           //     dayDataEntity.setBaiduActivity(getActive(dayDataEntity.getChannel(), beginTime, endTime, null, "_baidu", null, false));

//                dayDataEntity.setDouyinActivity(getActive(dayDataEntity.getChannel(), beginTime, endTime, null, "_topon", null, false));

                dayDataEntity.setMilanbarActivity(getActive("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_milanbar", null, false));


                dayDataEntity.setQqUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_qq", null, false));


                dayDataEntity.setWxUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_wechat", null, false));

                dayDataEntity.setMmyUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_mmy", null, false));


                dayDataEntity.setIqiyiUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_iqiyi", null, true));

                dayDataEntity.setIqiyih5UserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_iqiyih5", null, false));


                dayDataEntity.setAliUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_alipay", null, false));



                dayDataEntity.setBytedanceUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_tiktok", null, false));

                dayDataEntity.setSevenkUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_7k7k", null, false));


//                dayDataEntity.setDouyinUserCount(getNewUser(dayDataEntity.getChannel(), beginTime, endTime, null, "_topon", null, false));


          //      dayDataEntity.setBaiduUserCount(getNewUser(dayDataEntity.getChannel(), beginTime, endTime, null, "_baidu", null, false));

                dayDataEntity.setMeituanUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_meituan", null, true));

                dayDataEntity.setMeituanReUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_meituan_re", null, false));

                //红中麻将彩票日新增
                dayDataEntity.setCaipiaoUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_500caipiao", null, false));

                //红中麻将oppo-h5日新增
                dayDataEntity.setOppoUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_oppo", null, false));

                //红中麻将oppo日新增
                dayDataEntity.setOppoMbUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_oppo", null, false));

                //红中麻将京东日新增
                dayDataEntity.setJdUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_jd", null, false));

                //红中麻将顺网日新增
                dayDataEntity.setSwjoyUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_swjoy", null, false));

                //红中麻将闪电玩日活跃
                dayDataEntity.setSdwUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_sdwh5", null, false));

                //红中麻将苹果渠道日活跃
                dayDataEntity.setAppleUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_official_ios", null, false));

                //红中麻将华为日活跃
                dayDataEntity.setHuaweiUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_huawei", null, false));

                //红中麻将华为H5日活跃
                dayDataEntity.setHuaweiH5UserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_huawei", null, false));

                dayDataEntity.setMilanbarUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, "h5", "_milanbar", null, false));


                dayDataEntity.setTvhuanUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_tvhuan", null, false));

                dayDataEntity.setTvhisenseUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_tvhisense", null, false));


                dayDataEntity.setTvcoocaaUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_tvcoocaa", null, false));

                //红中麻将小米日活跃
                dayDataEntity.setMiUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_xiaomi", null, false));

                //红中麻将vivo日活跃
                dayDataEntity.setVivoUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_vivo", null, true));

                //红中麻将vivoad日活跃
                dayDataEntity.setVivoAdUserCount(getNewUser("hzmj",dayDataEntity.getChannel(), beginTime, endTime, null, "_vivoad", null, false));

//                dayDataEntity.setOfficialUserCount(getNewUser(dayDataEntity.getChannel(), beginTime, endTime, null, "_official", null, true));

            }
            //日新增付费用户总数,日新增用户充值总额
            Criteria criteria = new Criteria();
            criteria.and("reg_time").gte(beginTime).lt(endTime);
            if(sysDictDetail.getValue().equals("total")||sysDictDetail.getValue().equals("totalcl")||sysDictDetail.getValue().equals("totalapp")){}
            else {
                criteria.and("channel").regex(sysDictDetail.getValue().split("_")[0]);
            }
            criteria.and("all_fee").gt(0);
            Aggregation aggregation = Aggregation.newAggregation(
                    Aggregation.match(criteria),
                    Aggregation.group().count().as("count").sum("all_fee").as("totalFee")
            );
            AggregationResults<OrderEntity> ar ;
            if(sysDictDetail.getDictSort()==998||sysDictDetail.getDictSort()==910){
                ar = fiveMongoTemplate.aggregate(aggregation, "user", OrderEntity.class);
            } else  if(sysDictDetail.getDictSort()==997||sysDictDetail.getDictSort()==920){
                ar = ninethMongoTemplate.aggregate(aggregation, "user", OrderEntity.class);
            }
            else {
                ar = mongoTemplate.aggregate(aggregation, "user", OrderEntity.class);
            }

            List<OrderEntity> list = ar.getMappedResults();
            Integer count=0;
            if (list.size() > 0) {
                count = list.get(0).getCount().intValue();
                dayDataEntity.setDayNewDayPayNum(list.get(0).getTotalFee());
            }
            dayDataEntity.setDayNewPayCount(count);

            //日付费用户总数
            criteria = new Criteria();
            criteria.and("time_end").gte(beginTime).lt(endTime);
            if(sysDictDetail.getValue().equals("total")||sysDictDetail.getValue().equals("totalcl")||sysDictDetail.getValue().equals("totalapp")){}
            else {
                criteria.and("channel").regex(sysDictDetail.getValue().split("_")[0]);
            }
            aggregation = Aggregation.newAggregation(
                    Aggregation.match(criteria),
                    Aggregation.group("id").count().as("count")
            );
            if(sysDictDetail.getDictSort()==998||sysDictDetail.getDictSort()==910){

                ar = fiveMongoTemplate.aggregate(aggregation, "order", OrderEntity.class);
            } else if(sysDictDetail.getDictSort()==997||sysDictDetail.getDictSort()==920){

                ar = ninethMongoTemplate.aggregate(aggregation, "order", OrderEntity.class);
            }
            else {
                ar = mongoTemplate.aggregate(aggregation, "order", OrderEntity.class);
            }
            list = ar.getMappedResults();
            count = 0;
            if (list.size() > 0) {
                count = list.get(0).getCount().intValue();
            }
            dayDataEntity.setDayNewDayNum(count);

            dayDataEntity.setPangolin(0.0);
            dayDataEntity.setYoulianghui(0.0);
            dayDataEntity.setVivo(0.0);
            dayDataEntity.setVivoAd(0.0);
            dayDataEntity.setMi(0.0);
            dayDataEntity.setHuawei(0.0);
            dayDataEntity.setIOS(0.0);
            dayDataEntity.setOppoH5(0.0);
            dayDataEntity.setOppo(0.0);
            dayDataEntity.setChuanshanjia(0.0);
            dayDataEntity.setDouyin(0.0);
            dayDataEntity.setSevenk(0.0);
            dayDataEntity.setMmy(0.0);
            dayDataEntity.setIqiyiH5(0.0);
            Update update=new Update();
            Query query = new Query();
            query.addCriteria(Criteria.where("time").is(dayDataEntity.getTime()).and("channel").is(dayDataEntity.getChannel()));
            DayDataEntity dayDataEntity1= mongoTemplate.findOne(query,DayDataEntity.class);
            if(dayDataEntity1==null){
                fourthMongoTemplate.insert(dayDataEntity);
            }else {
               // update.set("dayPayCount",dayDataEntity.getDayPayCount());
                update.set("dayNewPayCount",dayDataEntity.getDayNewPayCount());
                update.set("dayNewDayNum",dayDataEntity.getDayNewDayNum());
                update.set("dayNewDayPayNum",dayDataEntity.getDayNewDayPayNum());
                update.set("allMoney",dayDataEntity.getAllMoney());
                update.set("hzmjMoney",dayDataEntity.getHzmjMoney());
                update.set("pcHzmjMoney",dayDataEntity.getPcHzmjMoney());
                update.set("appleHzmjMoney",dayDataEntity.getAppleHzmjMoney());
                update.set("qqMiniGamePay",dayDataEntity.getQqMiniGamePay());
                update.set("wxMiniGamePay",dayDataEntity.getWxMiniGamePay());
                update.set("meituanPay",dayDataEntity.getMeituanPay());
                update.set("meituanRePay",dayDataEntity.getMeituanRePay());
                update.set("oppoPay",dayDataEntity.getOppoPay());
                update.set("swjoyPay",dayDataEntity.getSwjoyPay());
                update.set("swjoyPcPay",dayDataEntity.getSwjoyPcPay());
                update.set("vivoPay",dayDataEntity.getVivoPay());
                update.set("vivoAdPay",dayDataEntity.getVivoAdPay());
                update.set("bytedancepay",dayDataEntity.getBytedancepay());
                update.set("sdwpay",dayDataEntity.getSdwpay());
                update.set("mipay",dayDataEntity.getMipay());
                update.set("huaweipay",dayDataEntity.getHuaweipay());
                update.set("applepay",dayDataEntity.getApplepay());
                update.set("oppoMbPay",dayDataEntity.getOppoMbPay());
                update.set("jdPay",dayDataEntity.getJdPay());
                update.set("kuaishoupay",dayDataEntity.getKuaishoupay());
                update.set("androidHzmjMoney",dayDataEntity.getAndroidHzmjMoney());
                update.set("dayActivity",dayDataEntity.getDayActivity());
                update.set("pcActivity",dayDataEntity.getPcActivity());
                update.set("iosActivity",dayDataEntity.getIosActivity());
                update.set("androidActivity",dayDataEntity.getAndroidActivity());
                update.set("qqActivity",dayDataEntity.getQqActivity());
                update.set("wxActivity",dayDataEntity.getWxActivity());
                update.set("oppoActivity",dayDataEntity.getOppoActivity());
                update.set("swjoyActivity",dayDataEntity.getSwjoyActivity());
                update.set("swjoyPcActivity",dayDataEntity.getSwjoyPcActivity());
                update.set("vivoActivity",dayDataEntity.getVivoActivity());
                update.set("vivoAdActivity",dayDataEntity.getVivoAdActivity());
                update.set("sdwActivity",dayDataEntity.getSdwActivity());
                update.set("meituanActivity",dayDataEntity.getMeituanActivity());
                update.set("meituanReActivity",dayDataEntity.getMeituanReActivity());
                update.set("miActivity",dayDataEntity.getMiActivity());
                update.set("huaweiActivity",dayDataEntity.getHuaweiActivity());
                update.set("appleActivity",dayDataEntity.getAppleActivity());
                update.set("oppoMbActivity",dayDataEntity.getOppoMbActivity());
                update.set("jdActivity",dayDataEntity.getJdActivity());
                update.set("kuaishoulActivity",dayDataEntity.getKuaishouActivity());
                update.set("bytedanceActivity",dayDataEntity.getBytedanceActivity());
                update.set("newUserCount",dayDataEntity.getNewUserCount());
                update.set("pcUserCount",dayDataEntity.getPcUserCount());
                update.set("iosnewUser",dayDataEntity.getIosnewUser());
                update.set("androidnewUser",dayDataEntity.getAndroidnewUser());
                update.set("qqUserCount",dayDataEntity.getQqUserCount());
                update.set("wxUserCount",dayDataEntity.getWxUserCount());
                update.set("meituanUserCount",dayDataEntity.getMeituanUserCount());
                update.set("meituanReUserCount",dayDataEntity.getMeituanReUserCount());
                update.set("oppoUserCount",dayDataEntity.getOppoUserCount());
                update.set("swjoyUserCount",dayDataEntity.getSwjoyUserCount());
                update.set("swjoyPcUserCount",dayDataEntity.getSwjoyPcUserCount());
                update.set("vivoUserCount",dayDataEntity.getVivoUserCount());
                update.set("vivoAdUserCount",dayDataEntity.getVivoAdUserCount());
                update.set("sdwUserCount",dayDataEntity.getSdwUserCount());
                update.set("miUserCount",dayDataEntity.getMiUserCount());
                update.set("huaweiUserCount",dayDataEntity.getHuaweiUserCount());
                update.set("appleUserCount",dayDataEntity.getAppleUserCount());
                update.set("oppoMbUserCount",dayDataEntity.getOppoMbUserCount());
                update.set("jdUserCount",dayDataEntity.getJdUserCount());
                update.set("kuaishouUserCount",dayDataEntity.getKuaishouUserCount());
                update.set("bytedanceUserCount",dayDataEntity.getBytedanceUserCount());
                update.set("iqiyiPay",dayDataEntity.getIqiyiPay());
                update.set("iqiyih5pay",dayDataEntity.getIqiyih5pay());
                update.set("alipay",dayDataEntity.getAlipay());
                update.set("xunleipay",dayDataEntity.getXunleipay());
                update.set("baidupay",dayDataEntity.getBaidupay());
                update.set("iosapppay",dayDataEntity.getIosapppay());
                update.set("douyinpay",dayDataEntity.getDouyinpay());
                update.set("tvcoocaapay",dayDataEntity.getTvcoocaapay());
                update.set("tvhisensepay",dayDataEntity.getTvhisensepay());
                update.set("tvhuanpay",dayDataEntity.getTvhuanpay());
                update.set("mmypay",dayDataEntity.getMmypay());
                update.set("sevenkpay",dayDataEntity.getSevenkpay());
                update.set("caipiaopay",dayDataEntity.getCaipiaopay());
                update.set("iqiyiActivity",dayDataEntity.getIqiyiActivity());
                update.set("iqiyih5Activity",dayDataEntity.getIqiyih5Activity());
                update.set("aliActivity",dayDataEntity.getAliActivity());
                update.set("xunleiActivity",dayDataEntity.getXunleiActivity());
                update.set("iosappActivity",dayDataEntity.getIosappActivity());
                update.set("douyinActivity",dayDataEntity.getDouyinActivity());
                update.set("tvcoocaaActivity",dayDataEntity.getTvcoocaaActivity());
                update.set("tvhisenseActivity",dayDataEntity.getTvhisenseActivity());
                update.set("tvhuanActivity",dayDataEntity.getTvhuanActivity());
                update.set("mmyActivity",dayDataEntity.getMmyActivity());
                update.set("sevenkActivity",dayDataEntity.getSevenkActivity());
                update.set("caipiaoActivity",dayDataEntity.getCaipiaoActivity());
                update.set("iqiyiUserCount",dayDataEntity.getIqiyiUserCount());
                update.set("iqiyih5UserCount",dayDataEntity.getIqiyih5UserCount());
                update.set("aliUserCount",dayDataEntity.getAliUserCount());
                update.set("xunleiUserCount",dayDataEntity.getXunleiUserCount());
                update.set("iosappUserCount",dayDataEntity.getIosappUserCount());
                update.set("douyinUserCount",dayDataEntity.getDouyinUserCount());
                update.set("tvcoocaaUserCount",dayDataEntity.getTvcoocaaUserCount());
                update.set("tvhisenseUserCount",dayDataEntity.getTvhisenseUserCount());
                update.set("tvhuanUserCount",dayDataEntity.getTvhuanUserCount());
                update.set("mmyUserCount",dayDataEntity.getMmyUserCount());
                update.set("sevenkUserCount",dayDataEntity.getSevenkUserCount());
                update.set("caipiaoUserCount",dayDataEntity.getCaipiaoUserCount());
                update.set("huaweih5pay",dayDataEntity.getHuaweih5pay());
                update.set("huaweiH5Activity",dayDataEntity.getHuaweiH5Activity());
                update.set("huaweiH5UserCount",dayDataEntity.getHuaweiH5UserCount());
                update.set("milanbarpay",dayDataEntity.getMilanbarpay());
                update.set("milanbarActivity",dayDataEntity.getMilanbarActivity());
                update.set("milanbarUserCount",dayDataEntity.getMilanbarUserCount());
                update.set("lfjpay",dayDataEntity.getLfjpay());
                update.set("lfjActivity",dayDataEntity.getLfjActivity());
                update.set("lfjUserCount",dayDataEntity.getLfjUserCount());
                update.set("qihoopay",dayDataEntity.getQihoopay());
                update.set("qihooActivity",dayDataEntity.getQihooActivity());
                update.set("qihooUserCount",dayDataEntity.getQihooUserCount());
                update.set("shengyepay",dayDataEntity.getShengyepay());
                update.set("shengyeActivity",dayDataEntity.getShengyeActivity());
                update.set("shengyeUserCount",dayDataEntity.getShengyeUserCount());
                update.set("alipay",dayDataEntity.getAlipay());
                update.set("aliActivity",dayDataEntity.getAliActivity());
                update.set("aliUserCount",dayDataEntity.getAliUserCount());
                update.set("quickpay",dayDataEntity.getQuickpay());
                update.set("quickActivity",dayDataEntity.getQuickActivity());
                update.set("quickUserCount",dayDataEntity.getQuickUserCount());
                update.set("zuiyoupay",dayDataEntity.getZuiyoupay());
                update.set("zuiyouActivity",dayDataEntity.getZuiyouActivity());
                update.set("zuiyouUserCount",dayDataEntity.getZuiyouUserCount());
                fourthMongoTemplate.updateFirst(query,update,DayDataEntity.class);
            }
        }
    }
    private Double getMoney(String flag,String channel,Long beginTime,Long endTime,String first,String suffix,String os,Boolean isSuff){
        Criteria criteria = new Criteria();
        criteria.and("time_end").gte(beginTime).lt(endTime);
        if(os!=null){
            criteria.and("os").is(os);
        }
        if(channel.indexOf("total")==0){
            if(suffix!=null) {
                if(first==null) {
                    if(isSuff) {
                        criteria.and("channel").regex("^(?!h5).*" + suffix+"$");
                    }else {
                        criteria.and("channel").regex("^(?!h5).*" + suffix);
                    }
                }else {
                    if(isSuff) {
                        criteria.and("channel").regex("h5.*" + suffix+"$");
                    }else {
                        criteria.and("channel").regex("h5.*" + suffix);
                    }
                }
            }
        }
        else {
            if(suffix!=null) {
                if(first!=null&&first.equals("h5")){
                    criteria.and("channel").is("h5"+channel.split("_")[0] + suffix);
                }
                else {
                    criteria.and("channel").is(channel.split("_")[0] + suffix);
                }
            }else {
                criteria.and("channel").regex(channel.split("_")[0]);
            }
        }
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group().sum("total_fee").as("v9")
        );
        AggregationResults<StaticeEntity> ar;
        if(flag.equals("clmj")||channel.equals("totalcl")){
           ar = fiveMongoTemplate.aggregate(aggregation, "order", StaticeEntity.class);
        }else if(flag.equals("app")||channel.equals("totalapp")){
            ar = ninethMongoTemplate.aggregate(aggregation, "order", StaticeEntity.class);
        }else {
            ar = mongoTemplate.aggregate(aggregation, "order", StaticeEntity.class);
        }
        List<StaticeEntity> list = ar.getMappedResults();
        if (list.size() > 0) {
            return list.get(0).getV9()/100;
        }
        return 0.0;
    }

    private Integer getActive(String flag,String channel,Long beginTime,Long endTime,String first,String suffix,String os,Boolean isSuff){

        Criteria criteria = new Criteria();
        criteria.and("last_time").gte(beginTime).lt(endTime);
        if(os!=null) {
            criteria.and("os").is(os);
        }
        if(channel.indexOf("total")==0){
            if(suffix!=null) {
                if(first==null) {
                    if(isSuff) {
                        criteria.and("channel").regex("^(?!h5).*" + suffix+"$");
                    }else {
                        criteria.and("channel").regex("^(?!h5).*" + suffix);
                    }
                }else {
                    if(isSuff) {
                        criteria.and("channel").regex("h5.*" + suffix+"$");
                    }else {
                        criteria.and("channel").regex("h5.*" + suffix);
                    }
                }
            }
        }
        else {
            if(suffix!=null) {
                if(first!=null&&first.equals("h5")){
                        criteria.and("channel").is("h5" + channel.split("_")[0] + suffix);
                }
                else {
                    criteria.and("channel").is(channel.split("_")[0] + suffix);
                }
            }else {
                criteria.and("channel").regex(channel.split("_")[0]);
            }
        }
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group().count().as("v1")
        );
        AggregationResults<StaticeEntity> ar;
        if(flag.equals("clmj")||channel.equals("totalcl")){
            ar = fiveMongoTemplate.aggregate(aggregation, "user", StaticeEntity.class);
        }else if(flag.equals("app")||channel.equals("totalapp")){
            ar=ninethMongoTemplate.aggregate(aggregation, "user", StaticeEntity.class);
        }
        else {
            ar = mongoTemplate.aggregate(aggregation, "user", StaticeEntity.class);
        }
        List<StaticeEntity>  list = ar.getMappedResults();
        if (list.size() > 0) {
            return list.get(0).getV1();
        }
        return 0;
    }

    private Integer getNewUser(String flag,String channel,Long beginTime,Long endTime,String first,String suffix,String os,Boolean isSuff){
        Criteria criteria = new Criteria();
        criteria.and("reg_time").gte(beginTime).lt(endTime);
        if(os!=null) {
            criteria.and("os").is(os);
        }
        if(channel.indexOf("total")==0){
            if(suffix!=null) {
                if(first==null) {
                    if(isSuff) {
                        criteria.and("channel").regex("^(?!h5).*" + suffix+"$");
                    }else {
                        criteria.and("channel").regex("^(?!h5).*" + suffix);
                    }
                }else {
                    if(isSuff) {
                        criteria.and("channel").regex("h5.*" + suffix+"$");
                    }else {
                        criteria.and("channel").regex("h5.*" + suffix);
                    }
                }
            }
        }
        else {
            if(suffix!=null) {
                if(first!=null&&first.equals("h5")){
                    criteria.and("channel").is("h5"+channel.split("_")[0] + suffix);
                }
                else {
                    criteria.and("channel").is(channel.split("_")[0] + suffix);
                }
            }else {
                criteria.and("channel").regex(channel.split("_")[0]);
            }
        }
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group().count().as("v1")
        );
        AggregationResults<StaticeEntity> ar;
        if(flag.equals("clmj")||channel.equals("totalcl")){
            ar = fiveMongoTemplate.aggregate(aggregation, "user", StaticeEntity.class);
        }else if(flag.equals("app")||channel.equals("totalapp")){
            ar = ninethMongoTemplate.aggregate(aggregation, "user", StaticeEntity.class);
        }else {
            ar = mongoTemplate.aggregate(aggregation, "user", StaticeEntity.class);
        }
        List<StaticeEntity> list = ar.getMappedResults();
        if (list.size() > 0) {
          return list.get(0).getV1();
        }
        return 0;
    }
}
